import { createRouter, createWebHashHistory } from 'vue-router'

import Login from '../views/LoginView'
import Home from '../views/HomeView'
import Welcome from '../components/WelcomeView'
import User from '../components/user/UserView'
import ShopOrder from '../components/order/ShopOrder'
import WasteOrder from '../components/order/WasteOrder'
import AlPublic from '../components/public/UserPublic'
import Public from '../components/public/PublicCate'
import PublicEvent from '../components/public/PublicEvent'
import Shop from '../components/shop/ShopView'
import Waste from '../components/waste/WasteView'
import WastePrice from '../components/waste/WastePrice'
import Notice from '../components/notice/NoticeView'
import Face from '../components/face/FaceList'
import FaceLog from '../components/face/FaceLog'
const routes = [
  { path: '/', redirect: '/home' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/user', component: User },
      { path: '/shoporder', component: ShopOrder },
      { path: '/wasteorder', component: WasteOrder },
      { path: '/alpublic', component: AlPublic },
      { path: '/publiccate', component: Public },
      { path: '/publicevent', component: PublicEvent },
      { path: '/shop', component: Shop },
      { path: '/waste', component: Waste },
      { path: '/wasteprice', component: WastePrice },
      { path: '/notice', component: Notice },
      { path: '/face', component: Face },
      { path: '/facelog', component: FaceLog }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  if (to.path !== '/login') {
    const token = window.sessionStorage.getItem('face_token')
    if (!token) {
      // token没有值
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
